package com.digitalchina.lxnsy.activity.management;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.LoginGuideActivity;
import com.digitalchina.lxnsy.activity.old.StandardProductionActivity;
import com.digitalchina.lxnsy.activity.management.message.MessageListActivity;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.EnterpriseInfoVO;
import com.digitalchina.lxnsy.entity.MsgListVO;
import com.digitalchina.lxnsy.entity.PermissionListVO;
import com.digitalchina.lxnsy.toolkit.ActivityTaskManager;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.SharedPreferencesUtil;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.lzy.okhttputils.model.HttpParams;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/23 15:12
 * 修改备注：
 * ================================================
 */
public class ManagementDetailsV2Activity extends BaseActivity {


    @Bind(R.id.tv_enterprise_name)
    TextView tvEnterpriseName;
    @Bind(R.id.layout_1)
    RelativeLayout layout1;
    @Bind(R.id.tv_msg_name)
    TextView tvMsgName;
    @Bind(R.id.tv_msg_time)
    TextView tvMsgTime;
    @Bind(R.id.tv_msg_operation)
    TextView tvMsgOperation;
    @Bind(R.id.layout_1_1)
    LinearLayout layout11;
    @Bind(R.id.layout_2)
    LinearLayout layout2;
    @Bind(R.id.layout_3)
    LinearLayout layout3;
    @Bind(R.id.layout_3_1)
    LinearLayout layout31;
    @Bind(R.id.layout_3_2)
    LinearLayout layout32;
    @Bind(R.id.layout_3_3)
    LinearLayout layout33;
    @Bind(R.id.layout_3_4)
    LinearLayout layout34;
    @Bind(R.id.layout_4)
    LinearLayout layout4;
    @Bind(R.id.layout_4_1)
    LinearLayout layout41;
    @Bind(R.id.layout_5)
    LinearLayout layout5;
    @Bind(R.id.layout_5_1)
    LinearLayout layout51;
    @Bind(R.id.layout_5_2)
    LinearLayout layout52;
    @Bind(R.id.layout_5_3)
    LinearLayout layout53;
    @Bind(R.id.layout_5_4)
    LinearLayout layout54;
    @Bind(R.id.tv_2_1)
    TextView tv21;
    @Bind(R.id.tv_2_2)
    TextView tv22;
    private List<PermissionListVO> voList;


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_management_details_v2);
        setTitle("农企（合作社）管理系统");
        if (AdminXML.getUserType(activity).equals("1"))
            setRightText("退出");

    }

    @Override
    public void initMember() {

        if (AdminXML.getUserType(activity).equals("1")) {
            // 管理员
            hideBackBtn();
        } else {
            // 普通用户
        }

        getUserPermission();

    }

    @Override
    public void addListener() {


    }


    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0:
                        voList = FastJsonUtil.getListBean(message.obj.toString(), "resourceList", PermissionListVO.class);
                        if (voList == null || voList.size() == 0)
                            return false;

                        EnterpriseInfoVO vo = FastJsonUtil.getBean(message.obj.toString(), "enterpriseInfo", EnterpriseInfoVO.class);
                        tvEnterpriseName.setText(vo.getName());
                        setView();
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                    case MSG_S_1:
                        List<MsgListVO> list = FastJsonUtil.getListBean(message.obj.toString(), "messageVOList", MsgListVO.class);
                        if (list != null && list.size() > 0) {
                            tvMsgName.setText(list.get(0).getMessageName());
                            tvMsgTime.setText(list.get(0).getMessageDate());
                            tvMsgOperation.setText(list.get(0).getOperaterName() + "操作");
                        } else {
                            tvMsgTime.setText("暂无数据");
                        }
                        break;
                }
                return false;
            }
        });
    }

    private void setView() {
        for (int i = 0; i < voList.size(); i++) {
            switch (voList.get(i).getName()) {
                case "消息":
                    layout1.setVisibility(View.VISIBLE);
                    layout11.setVisibility(View.VISIBLE);
                    getMsgList();
                    break;
                case "社员管理":
                    layout2.setVisibility(View.VISIBLE);
                    for (int j = 0; j < voList.get(i).getChildren().size(); j++) {
                        switch (voList.get(i).getChildren().get(j).getName()) {
                            case "基础信息":
                                tv21.setVisibility(View.VISIBLE);
                                break;
                            case "社员权限":
                                tv22.setVisibility(View.VISIBLE);
                                break;
                        }
                    }
                    break;
                case "农资进销存":
                    layout3.setVisibility(View.VISIBLE);
                    for (int j = 0; j < voList.get(i).getChildren().size(); j++) {
                        switch (voList.get(i).getChildren().get(j).getName()) {
                            case "农用品采购":
                                layout31.setVisibility(View.VISIBLE);
                                break;
                            case "种子采购":
                                layout32.setVisibility(View.VISIBLE);
                                break;
                            case "农资出入库":
                                layout33.setVisibility(View.VISIBLE);
                                break;
                            case "出库销售":
                                layout34.setVisibility(View.VISIBLE);
                                break;
                        }
                    }
                    break;
                case "农事操作":
                    layout4.setVisibility(View.VISIBLE);
                    layout41.setVisibility(View.VISIBLE);
                    break;
                case "统计分析":
                    layout5.setVisibility(View.VISIBLE);
                    for (int j = 0; j < voList.get(i).getChildren().size(); j++) {
                        switch (voList.get(i).getChildren().get(j).getName()) {
                            case "生产预测":
                                layout51.setVisibility(View.VISIBLE);
                                break;
                            case "销售预估":
                                layout52.setVisibility(View.VISIBLE);
                                break;
                            case "经营情况":
                                layout53.setVisibility(View.VISIBLE);
                                break;
                            case "农事统计":
                                layout54.setVisibility(View.VISIBLE);
                                break;
                        }
                    }
                    break;
            }
        }
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {
        loginOut();
    }

    private void loginOut() {
        if (AdminXML.getUserType(activity).equals("1")) {
            // 管理员
            new MaterialDialog.Builder(activity)
                    .title("提示")
                    .content("您确定要退出登录吗？")
                    .positiveText("确定")
                    .negativeText("取消")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            SharedPreferencesUtil.cleanXml(activity, AdminXML.NAME);
                            go(LoginGuideActivity.class);
                            ActivityTaskManager.getInstance().closeAllActivity();
                        }
                    })
                    .show();
        } else {
            // 普通用户
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void getUserPermission() {

        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        //params.put("resNo", AdminXML.getResNo(activity));
        params.put("userAccId", AdminXML.getUserAccId(activity));

        OkHttpUtil.post(activity, URL.URL_GET_USER_PERMISSION, "正在获取权限", params, mHandler,
                MSG_S_0, MSG_F_0);
    }

    @OnClick({R.id.tv_enterprise_name, R.id.layout_1, R.id.layout_1_1, R.id.tv_2_1, R.id.tv_2_2, R.id.layout_3_1,
            R.id.layout_3_2, R.id.layout_3_3, R.id.layout_3_4, R.id.layout_4_1, R.id.layout_5_1, R.id.layout_5_2,
            R.id.layout_5_3, R.id.layout_5_4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_enterprise_name:
                loginOut();
                break;
            case R.id.layout_1:
            case R.id.layout_1_1:
                //消息列表
                AdminXML.setResNo(activity, "nq_005_001");
                GotoUtil.gotoActivity(activity, MessageListActivity.class);
                break;
            case R.id.tv_2_1:
                //企业信息
                AdminXML.setResNo(activity, "nq_001_002");
                GotoUtil.gotoActivity(activity, BasicInfoActivity.class);
                break;
            case R.id.tv_2_2:
                //社员权限
                AdminXML.setResNo(activity, "nq_001_001");
                GotoUtil.gotoActivity(activity, MemberPermissionListActivity.class);
                break;
            case R.id.layout_3_1:
                //农用品采购
                AdminXML.setResNo(activity, "nq_002_001");
                GotoUtil.gotoActivity(activity, AddFarmProductActivity.class);
                break;
            case R.id.layout_3_2:
                //种子
                AdminXML.setResNo(activity, "nq_002_002");
                GotoUtil.gotoActivity(activity, AddSeedActivity.class);
                break;
            case R.id.layout_3_3:
                //农资出入库
                AdminXML.setResNo(activity, "nq_002_003");
                GotoUtil.gotoActivity(activity, AddOutInActivity.class);
                break;
            case R.id.layout_3_4:
                //出库销售
                AdminXML.setResNo(activity, "nq_002_004");
                GotoUtil.gotoActivity(activity, AddProductOutActivity.class);
                break;
            case R.id.layout_4_1:
                //农事操作
                AdminXML.setResNo(activity, "nq_003_001");
                AdminXML.setFarmOperation(activity, "1");
                GotoUtil.gotoActivity(activity, StandardProductionActivity.class);
                break;
            case R.id.layout_5_1:
                //生产预测
                AdminXML.setResNo(activity, "nq_004_001");
                GotoUtil.gotoActivity(activity, ProductionForecastActivity.class);
                break;
            case R.id.layout_5_2:
                //销售预估
                AdminXML.setResNo(activity, "nq_004_003");
                GotoUtil.gotoActivity(activity, SalesForecastActivity.class);
                break;
            case R.id.layout_5_3:
                //经营情况
                AdminXML.setResNo(activity, "nq_004_002");
                GotoUtil.gotoActivity(activity, BusinessSituationActivity.class);
                break;
            case R.id.layout_5_4:
                //农事统计
                AdminXML.setResNo(activity, "nq_004_004");
                GotoUtil.gotoActivity(activity, FarmingStatisticsActivity.class);
                break;
        }
    }

    public void getMsgList() {

        /**
         * enterpriseId	企业ID
         * type	类型（0 全部 /1 已经查看 /2 待查看）
         * userAccId	用户ID
         * pageNum	页数（第几页）
         * pageSize	每页数量
         */
        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", "nq_005_001");
        params.put("userAccId", AdminXML.getUserAccId(activity));
        params.put("type", "0");
        params.put("pageNum", String.valueOf(1));
        params.put("pageSize", String.valueOf(1));

        OkHttpUtil.post(activity, URL.URL_GET_MESSAGE_LIST, "", params, mHandler,
                MSG_S_1, MSG_F_1);
    }
}
